package com.woke.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class User {
	private int id;
	private String username;
	private String password;
	private String sex;
	private String tel;
	private String email;
	private String role;
	private String createtime;
	private String updatetime;
	private String isuser;
	
	
	public String getIsuser() {
		return isuser;
	}
	public void setIsuser(String isuser) {
		this.isuser = isuser;
	}
	public String getCreatetime() {
		return createtime;
	}
	public void setCreatetime(String createtime) {
		this.createtime = createtime;
	}
	public String getUpdatetime() {
		return updatetime;
	}
	public void setUpdatetime(String updatetime) {
		this.updatetime = updatetime;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getRole() {
		return role;
	}
	public void setRole(String role) {
		this.role = role;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public User() {

	}
	
	public User(int id, String username,String password,String sex,String tel,String email,String role,String createtime,String updatetime,String isuser) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.sex=sex;
		this.tel=tel;
		this.email=email;
		this.role = role;
		this.createtime=createtime;
		this.updatetime=updatetime;
		this.isuser=isuser;
	}

	public static List<User> getData(String sql) {
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		List<User> list = new ArrayList<User>();
		try {
			
			con = WokeConnection.getConnection();
			st = con.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int id = rs.getInt("id");
				String username = rs.getString("username");
				String password = rs.getString("password");
				String sex = rs.getString("sex");
				String tel = rs.getString("tel");
				String email = rs.getString("email");
				String role = rs.getString("role");
				String createtime = rs.getString("createtime");
				String updatetime = rs.getString("updatetime");
				String isuser = rs.getString("isuser");
				
				User u = new User(id, username, password, sex, tel, email, role, createtime, updatetime,isuser);
				list.add(u);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			WokeConnection.closeMySql(con, st, rs);
		}
		return list;
	}
	
	public int selectLendSize(){
		Connection conn = WokeConnection.getConnection();
		PreparedStatement pst=null;
		ResultSet rs=null;
		try {
			 pst = conn.prepareStatement("select count(*) from user");
			 rs = pst.executeQuery();
			if(rs.next()){
				int pagecount = rs.getInt(1);
				return pagecount;
			}
			return 0;
		} catch (Exception e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

			return 0;

		}finally{
			WokeConnection.closeMySql(conn, pst, rs);
		}
	}
	
	public List<User> selectLend(int pageNow, int pageSize){

		List<User> list = new ArrayList<User>();
		Connection conn = WokeConnection.getConnection();
		PreparedStatement pst=null;
		ResultSet rs=null;
		
		try {
			int startIndex =0;
			int endIndex = pageSize;
			String sql = "select  * from user ";
			String sw = " where " + " 1=1 " + " order by id Desc ";
			// sql +=" where " + strWhere ;
			if (pageNow > 1) {
				startIndex = pageSize * (pageNow - 1);
				// endIndex = pageSize * currentPage;
			}
			sql += sw + " limit " + startIndex + "," + endIndex;
			
			 pst= conn.prepareStatement(sql);
			 rs= pst.executeQuery();
			while(rs.next()){
				User s= new User();
				s.setId(rs.getInt(1));
				s.setUsername(rs.getString(2));
				s.setPassword(rs.getString(3));
				s.setSex(rs.getString(4));
				s.setTel(rs.getString(5));
				s.setEmail(rs.getString(6));
				s.setRole(rs.getString(7));
				s.setCreatetime(rs.getString(8));
				s.setUpdatetime(rs.getString(9));
				s.setIsuser(rs.getString(10));
				list.add(s);
			}
			return list;
		} catch (Exception e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

			return null;

		}finally{

			WokeConnection.closeMySql(conn, pst, rs);

		}

	}

	
}
